package com.gitee.wsl.ext.string

fun String.collapseSpaces(): String {
    val builder = StringBuilder()
    var haveSpaces = false
    for (c in this) {
        if (c.isWhitespace()) {
            haveSpaces = true
        } else {
            if (haveSpaces) {
                builder.append(" ")
                haveSpaces = false
            }
            builder.append(c)
        }
    }
    return builder.toString()
}

/** Right-align the string into a fixed-width space. */
fun String.rightAlign(width: Int): String = (" ".repeat(width) + this).let { it.substring(it.length - width) }
